//
//  MCPayWebViewController.m
//  TLYL
//
//  Created by MC on 2017/7/28.
//  Copyright © 2017年 TLYL01. All rights reserved.
//

#import "MCPayWebViewController.h"
#import <WebKit/WebKit.h>

@interface MCPayWebViewController ()<WKNavigationDelegate,WKUIDelegate>

@property (nonatomic,strong) WKWebView *webView;
@property (nonatomic,strong)UIActivityIndicatorView *activityIndicator;
@end

@implementation MCPayWebViewController
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [SVProgressHUD dismiss];
    [BKIndicationView dismiss];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self createUI];
    
    if (_strTitle) {
        self.title=_strTitle;
    }
    
    
}

-(void)createUI{
    
    self.view.backgroundColor=[UIColor whiteColor];
    WKWebView *webView = [[WKWebView alloc] init];
    [self.view addSubview:webView];
    [webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(0);
        make.left.and.right.equalTo(self.view).offset(0);
        make.bottom.equalTo(self.view.mas_bottom).offset(0);
    }];
    self.webView=webView;
    //    webView.scalesPageToFit=YES;
    
    // 2.创建URL
    NSURL *url = [self GetNSURLWithURL:_url];
    
    // 3.创建Request
    NSURLRequest *request =[NSURLRequest requestWithURL:url];
    // 4.加载网页
    [webView loadRequest:request];
    // 5.最后将webView添加到界面
    //    webView.delegate = self;
    webView.UIDelegate = self;
    webView.navigationDelegate = self;
    
    
    //创建UIActivityIndicatorView背底半透明View
    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, G_SCREENWIDTH, G_SCREENHEIGHT)];
    [view setTag:103];
    [view setBackgroundColor:RGB(150, 150, 150)];
    [view setAlpha:0.9];
    [self.view addSubview:view];
    
    _activityIndicator =[[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(0.0f, 0.0f, 32.0f, 32.0f)];
    _activityIndicator.color = RGB(255,109,154);
    _activityIndicator.transform = CGAffineTransformMakeScale(2.0f, 2.0f);
    [_activityIndicator setCenter:view.center];
    [_activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhite];
    [view addSubview:_activityIndicator];
    
    
    
}


//1.转换编码
-(NSURL *)GetNSURLWithURL:(NSString *)sUrl{
    //    sUrl = [sUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    //    NSURL *url = [NSURL URLWithString:[MCPayWebViewController returnFormatString:sUrl]];
    NSURL *url = [NSURL URLWithString:sUrl];
    
    return url;
}

//2.除去空格
+(NSString *)returnFormatString:(NSString *)str
{
    return [str stringByReplacingOccurrencesOfString:@" "withString:@" "];
}




/*浏览器后退*/
- (void)clickGoBackBtn{
    if(self.webView.canGoBack){
        [self.webView goBack];
    }
}
/*浏览器前进*/
- (void)clickGoForwardBtn{
    if(self.webView.canGoForward){
        [self.webView goForward];
    }
}
#pragma mark - UIWebViewDelegate代理方法
#pragma mark 开始加载
//是否允许加载网页，也可获取js要打开的url，通过截取此url可与js交互
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
 navigationType:(UIWebViewNavigationType)navigationType
{
    
    return YES;
}
////开始加载网页
//- (void)webViewDidStartLoad:(UIWebView *)webView{
// 页面开始加载时调用
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{
    //显示网络请求加载
    [UIApplication sharedApplication].networkActivityIndicatorVisible = true;
    [_activityIndicator startAnimating];
    
    
}
// 当内容开始返回时调用
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation{
}
// 在收到响应开始加载后，决定是否跳转
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler
{
    //返回支付宝的信息字符串，alipays:// 以后的为支付信息，这个信息后台是经过 URLEncode 后的，前端需要进行解码后才能跳转支付宝支付（坑点）
    
    //https://ds.alipay.com/?from=mobilecodec&scheme=alipays://platformapi/startapp?saId=10000007&clientVersion=3.7.0.0718&qrcode=https%253A%252F%252Fqr.alipay.com%252Fbax041244dd0qf8n6ras805b%253F_s%253Dweb-other
    
    NSString *urlStr = [navigationResponse.response.URL.absoluteString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    if ([urlStr containsString:@"alipays://"]) {
        
        NSRange range = [urlStr rangeOfString:@"alipays://"]; //截取的字符串起始位置
        NSString * resultStr = [urlStr substringFromIndex:range.location]; //截取字符串
        
        NSURL *alipayURL = [NSURL URLWithString:resultStr];
        
        [[UIApplication sharedApplication] openURL:alipayURL options:@{UIApplicationOpenURLOptionUniversalLinksOnly: @NO} completionHandler:^(BOOL success) {
            
        }];
    }
    WKNavigationResponsePolicy actionPolicy = WKNavigationResponsePolicyAllow;
    //这句是必须加上的，不然会异常
    decisionHandler(actionPolicy);
}

//// 在收到响应开始加载后，决定是否跳转
//- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler
//{
//
//    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"alipay://alipayclient/?%@",navigationResponse.response.URL.absoluteString]] options:@{UIApplicationOpenURLOptionUniversalLinksOnly: @NO} completionHandler:^(BOOL success) {
//
//    }];
//    WKNavigationResponsePolicy actionPolicy = WKNavigationResponsePolicyAllow;
//    //这句是必须加上的，不然会异常
//    decisionHandler(actionPolicy);
//}

// 页面加载完成之后调用
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
    
    ////网页加载完成
    //- (void)webViewDidFinishLoad:(UIWebView *)webView{
    //隐藏网络请求加载图标
    [UIApplication sharedApplication].networkActivityIndicatorVisible = false;
    
    
    
    [_activityIndicator stopAnimating];
    UIView *view=(UIView *)[self.view viewWithTag:103];
    [view removeFromSuperview];
    
    if (!_strTitle) {
        //取得html内容
        //        self.title=[webView stringByEvaluatingJavaScriptFromString:@"document.title"];
        //        NSLog(@"%@",[webView stringByEvaluatingJavaScriptFromString:@"document.title"]);
        
    }
}
//网页加载错误
// 页面加载失败时调用
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation{
    //- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    
    
    [_activityIndicator stopAnimating];
    UIView *view=(UIView *)[self.view viewWithTag:103];
    [view removeFromSuperview];
    
    //    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"系统提示"
    //                                                    message:@"网络连接发生错误!"
    //                                                   delegate:self
    //                                          cancelButtonTitle:nil
    //                                          otherButtonTitles:@"确定", nil];
    //    [alert show];
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

